package com.pan.admin.module.loanConfig.service.impl;

import com.gitee.apanlh.util.valid.ValidParam;
import com.pan.admin.common.utils.DateUtils;
import com.pan.admin.common.utils.SecurityUtils;
import com.pan.admin.module.channel.entity.vo.ElementLabelVO;
import com.pan.admin.module.channel.entity.vo.ElementTreeVO;
import com.pan.admin.module.loanConfig.dao.LoanRegisterFieldMapper;
import com.pan.admin.module.loanConfig.entity.LoanRegisterField;
import com.pan.admin.module.loanConfig.service.LoanRegisterFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 用户注册要素字段配置Service业务层处理
 *
 * @author ts
 * @date 2023-09-08
 */
@Service
public class LoanRegisterFieldServiceImpl implements LoanRegisterFieldService {
    @Autowired
    private LoanRegisterFieldMapper loanRegisterFieldMapper;

    /**
     * 查询用户注册要素字段配置
     *
     * @param id 用户注册要素字段配置主键
     * @return 用户注册要素字段配置
     */
    @Override
    public LoanRegisterField selectLoanRegisterFieldById(Long id) {
        return loanRegisterFieldMapper.selectLoanRegisterFieldById(id);
    }

    /**
     * 查询注册要素字段配置树控列表
     */
    @Override
    public List<ElementTreeVO> treeList(LoanRegisterField loanRegisterField) {
        List<LoanRegisterField> list = loanRegisterFieldMapper.selectLoanRegisterFieldList(loanRegisterField);

        List<ElementTreeVO> treeVOList = new ArrayList<>();
        if (!ValidParam.isEmpty(list)) {
            ElementTreeVO treeVO = new ElementTreeVO();
            List<ElementLabelVO> labelVOList = new ArrayList<>();

            for (LoanRegisterField field : list) {
                ElementLabelVO labelVO = new ElementLabelVO();
                labelVO.setLabel(field.getName());
                labelVO.setValue(field.getId());
                labelVOList.add(labelVO);
            }

            treeVO.setList(labelVOList);
            treeVO.setLabel("全选");
            treeVO.setValue(0L);
            treeVOList.add(treeVO);
        }
        return treeVOList;
    }


    /**
     * 查询用户注册要素字段配置列表
     *
     * @param loanRegisterField 用户注册要素字段配置
     * @return 用户注册要素字段配置
     */
    @Override
    public List<LoanRegisterField> selectLoanRegisterFieldList(LoanRegisterField loanRegisterField) {
        return loanRegisterFieldMapper.selectLoanRegisterFieldList(loanRegisterField);
    }

    /**
     * 新增用户注册要素字段配置
     *
     * @param loanRegisterField 用户注册要素字段配置
     * @return 结果
     */
    @Override
    public int insertLoanRegisterField(LoanRegisterField loanRegisterField) {
        loanRegisterField.setCreateTime(DateUtils.getNowDate());
        loanRegisterField.setCreateBy(SecurityUtils.getUsername());
        return loanRegisterFieldMapper.insertLoanRegisterField(loanRegisterField);
    }

    /**
     * 修改用户注册要素字段配置
     *
     * @param loanRegisterField 用户注册要素字段配置
     * @return 结果
     */
    @Override
    public int updateLoanRegisterField(LoanRegisterField loanRegisterField) {
        loanRegisterField.setUpdateTime(DateUtils.getNowDate());
        loanRegisterField.setUpdateBy(SecurityUtils.getUsername());
        return loanRegisterFieldMapper.updateLoanRegisterField(loanRegisterField);
    }


}
